﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Jb.Common.Extensions;
using Jb.Domain;
using Jb.Web.ViewModels;
using Jb.Web.Utility;
using AutoMapper;
using Jb.Web.ViewModels.UtilityModels;

namespace Jb.Web.Controllers
{
    public class PackageController : BaseController
    {
		private JbEntities _Context = new JbEntities();
        
		#region Constructors
		public PackageController()
		{
		}

		public PackageController(UserViewModel currentUser)
			: base(currentUser)
        {
        }
		#endregion Constructors

		#region Action Methods

        /// <summary>
        /// Index controller, show the list of packages as default
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View("List", BuildPackageListConfig());
        }

        /// <summary>
        /// List of packages
        /// </summary>
        /// <returns></returns>
		public ActionResult List()
        {
            return View(BuildPackageListConfig());
        }

        /// <summary>
        /// Shows the details of a package for subscription
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Subscribe(int id = 0, String isSaved = null, String isPost = null, String status = null)
        {
            Package entity = _Context.Packages.FirstOrDefault(P => P.ID == id);
            PackageViewModel viewModel = Mapper.Map<Package, PackageViewModel>(entity);            
            return View(viewModel);
        }

        /// <summary>
        /// Handles Post request for subscription process, if the request is for a free package
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Subscribe(int id)
        {
            Package entity = _Context.Packages.FirstOrDefault(P => P.ID == id);
            //PackageViewModel viewModel = Mapper.Map<Package, PackageViewModel>(entity);
            if (entity == null)
                return RedirectToAction("Subscribe", new {id = id, isPost = "true", isSaved = "false", status = 1 });

            if(entity.Price > 0)
                return RedirectToAction("Subscribe", new { id = id, isPost = "true", isSaved = "false", status = 2 });

            ///Direct sales with free package
            if (LoggedInUser != null && LoggedInUser.Contact != null)
            {
                var currentSubscription = LoggedInUser.EmployerInfo.Subscription;
                ///Check already subscribed to a package or not. If yes, then its a probable hacking attempt.
                if (currentSubscription != null && currentSubscription.CanPostJob && currentSubscription.CanDownloadResume)
                    return RedirectToAction("Subscribe", new { id = id, isPost = "true", isSaved = "false", status = 3 });
                
                PackageSubscription subscription = new PackageSubscription();
                subscription.CompanyID = LoggedInUser.Contact.CompanyID;
                subscription.PackageID = entity.ID;
                subscription.Duration = entity.Duration;
                subscription.MaxJobPost = entity.MaxJobPost;
                subscription.MaxResumeDownload = entity.MaxResumeDownload;
                subscription.SubscribedOn = DateTime.Now;
                subscription.IsActive = true;
                subscription.RefSalesOrderID = null;

                _Context.AddToPackageSubscriptions(subscription);
                _Context.SaveChanges();
                return RedirectToAction("Subscribe", new { id = id, isPost = "true", isSaved = "true", status = 10 });
            }
            return RedirectToAction("Subscribe", new { id = id, isPost = "true", isSaved = "false", status = 12 });
        }
		#endregion Action Methods
		
		#region Helper Methods

		/// <summary>
        /// Builds a ListConfig object with a list of Package
        /// </summary>
        /// <returns></returns>
		private ListConfig BuildPackageListConfig()
		{
			UtilityDAO dao = new UtilityDAO();
            Jb.Domain.DbParameter[] parameters = new[]{
                new Jb.Domain.DbParameter("@GetActiveOnly", true)
            };
            DataSet ds = dao.GetDataSet(DbObject.SP.GET_PACKAGE_LIST, parameters, true);
            ListConfig listConfig = new ListConfig(ds.Tables[0], null);
            return listConfig;
		}

        #endregion			
    }
}


